package com.mvetc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mvetc.dao.CountryDao;
import com.mvetc.entity.Country;
import com.mvetc.util.DBUtil;

public class CountryDaoImpl implements CountryDao {

	@Override
	public List<Country> findBy(Map<String, String> params) {
		List<Country> list = new ArrayList<>();
		ResultSet doQuery = null;
		StringBuffer sb = new StringBuffer();
		sb.append("select country_id")
		.append(",country_name")
		.append(" from country_info");
		if (!"".equals(params.get("countryId")) && "".equals(params.get("countryName"))) {
			sb.append(" where country_id = ?");
			doQuery = DBUtil.doQuery(sb.toString(), params.get("countryId"));
		}else if ("".equals(params.get("countryId")) && !"".equals(params.get("countryName"))) {
			sb.append(" where country_name = ?");
			doQuery = DBUtil.doQuery(sb.toString(), params.get("countryName"));
		}else {
			doQuery = DBUtil.doQuery(sb.toString());
		}
		try {
			while (doQuery.next()) {
				String countryId = doQuery.getString("country_id");
				String countryName = doQuery.getString("country_name");
				Country country = new Country(countryId, countryName, null);
				list.add(country);	
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

}
